package com.yanqu.road.server.agent.inter;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.admin.AdminResult;
import com.yanqu.road.entity.admin.AdminResult2;
import com.yanqu.road.entity.enums.eAgentErrorCode;
import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.entity.union.cross.CrossUnionServerGroup;
import com.yanqu.road.server.agent.AgentInterfaceMgr;
import com.yanqu.road.server.manager.union.CrossUnionConfigMgr;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;

/**
 * wmm
 */
public class AgentUnion {

    public String unionSearch(JSONObject param) {
        JSONObject ret = AgentInterfaceMgr.getDefaultRet();

        int channelId = param.getIntValue("channelId");
        long serverId = param.getLongValue("serverId");

        int pageNum = param.getIntValue("pageNum");
        int pageSize = param.getIntValue("pageSize");
        String unionUid = param.getString("monsterLeagueId");
        String unionName = param.getString("monsterLeagueName");
        String orderSortHT = param.getString("orderSortHT");
        String orderByColumnHT = param.getString("orderByColumnHT");

        List<CrossUnionInfo> resultUnionInfos = new ArrayList<>();

        if(!StringUtils.isNullOrEmpty(unionUid)){
            CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfoMap().get(unionUid);
            if(unionInfo == null){
                unionInfo = CrossUnionMgr.getDeleteUnionInfoMap().get(unionUid);
                if(unionInfo != null){
                    resultUnionInfos.add(unionInfo);
                }
            }
        }else if(!StringUtils.isNullOrEmpty(unionName)){
            List<CrossUnionInfo> allList = new ArrayList<>();
            allList.addAll(CrossUnionMgr.getUnionInfoMap().values());
            allList.addAll(CrossUnionMgr.getDeleteUnionInfoMap().values());
            for (CrossUnionInfo unionInfo : allList) {
                if (!unionInfo.getUnionName().contains(unionName)) {
                    continue;
                }
                resultUnionInfos.add(unionInfo);
            }
        }else {
            List<CrossUnionInfo> allList = new ArrayList<>();
            allList.addAll(CrossUnionMgr.getUnionInfoMap().values());
            allList.addAll(CrossUnionMgr.getDeleteUnionInfoMap().values());
            resultUnionInfos = allList;
        }


        if("asc".equals(orderSortHT)){
            resultUnionInfos.sort(Comparator.comparing(UnionInfo::getTotalEarnSpeed));
        }else {
            resultUnionInfos.sort((o1, o2) -> o2.getTotalEarnSpeed().compareTo(o1.getTotalEarnSpeed()));
        }

        List<CrossUnionInfo> pageUnionInfos = new ArrayList<>();
        int startIndex = (pageNum - 1) * pageSize;
        startIndex = Math.max(0, startIndex);

        for (int i = startIndex; i < startIndex + pageSize && i < resultUnionInfos.size(); i++) {
            pageUnionInfos.add(resultUnionInfos.get(i));
        }

        List<Map<String, Object>> list = new ArrayList<>();
        AdminResult adminResult = AdminResult.defaultInstance();
        adminResult.getData().put("total", pageUnionInfos.size());
        for (CrossUnionInfo unionInfo : pageUnionInfos) {
            Map<String, Object> map = new HashMap<>();
            map.put("channelId", channelId);
            map.put("serverId", unionInfo.getServerId());
            map.put("monsterLeagueId", unionInfo.getUnionUid());
            map.put("monsterLeagueFlag", unionInfo.getUnionFlagStr());
            map.put("monsterLeagueName", unionInfo.getUnionName());
            map.put("leagueMasterId", unionInfo.getMasterUserId());
            map.put("randomlyJoin", unionInfo.isAllowRandomJoin());
            map.put("monsterLeagueAllPower", unionInfo.getTotalEarnSpeed().toString());
            CrossUnionServerGroup group = CrossUnionServerGroupMgr.getServerGroupByServerId(unionInfo.getServerId());
            int groupCount = 1;
            if(group != null && group.getState() == 1){
                groupCount = group.getServerList().size();
            }
            map.put("joinLimit", CrossUnionConfigMgr.getMaxMemberCount(unionInfo.getExperience(), unionInfo.isCross(), groupCount));
            map.put("notice", unionInfo.getOutNotice());
            map.put("declaration", unionInfo.getInnerNotice());
            map.put("recruitInfo", Collections.emptyList());
            map.put("leaderWechat", unionInfo.getWechatCode());
            map.put("level", CrossUnionConfigMgr.getUnionLv(unionInfo.getExperience()));
            map.put("status", unionInfo.isDelete() ? 0 : 1);
            list.add(map);
        }

        adminResult.getData().put("row", list);

        ret.put("result", adminResult.toJsonString());
        AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.Success.getValue());
        return ret.toJSONString();
    }

    public String unionMember(JSONObject param) {

        JSONObject ret = AgentInterfaceMgr.getDefaultRet();

        String unionUid = param.getString("monsterLeagueId");
        Map<Long, CrossUnionMember> memberMap = CrossUnionMgr.getUnionMemberMap(unionUid);
        if(memberMap == null){
            ret.put("msg", "找不到商会");
            return ret.toString();
        }
        List<CrossUnionMember> list = new ArrayList<>(memberMap.values());

        String orderSortHT = param.getString("orderSortHT");
        if("asc".equals(orderSortHT)){
            list.sort(Comparator.comparing(o -> o.getUserBaseInfo().getEarnSpeed()));
        }else {
            list.sort((o1, o2) -> o2.getUserBaseInfo().getEarnSpeed().compareTo(o1.getUserBaseInfo().getEarnSpeed()));
        }
        AdminResult2 adminResult = AdminResult2.defaultInstance();
        for (CrossUnionMember member : list) {
            Map<String,Object> object = new HashMap<>();
            object.put("userId", member.getUserId());
            object.put("userName", member.getUserBaseInfo().getNickName());
            object.put("level", member.getUserBaseInfo().getTitleId());
            object.put("power", member.getUserBaseInfo().getEarnSpeed().toString());
            object.put("jobTitle", eUnionPosition.getPositionName(member.getPosition()));
            object.put("openId", "");
//                object.put("todayCont", member.getTodayContribution());
//                object.put("quitTime", member.getQuitTime());
//                object.put("kickTime", member.getKickTime());
//                object.put("joinTime", member.getJoinTime());
//                object.put("dayKickNum", member.getDayKickNum());
//                object.put("historyMaxAbility", member.getHistoryMaxAbility().toString());
            adminResult.getData().add(object);
        }
        ret.put("result", adminResult);
        AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.Success.getValue());
        return ret.toJSONString();

    }
}
